package org.xjtu.weteam.common.aspect;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.xjtu.weteam.common.exception.CustomException;
import org.xjtu.weteam.model.dto.ApiResponse;

/**
 * 全局异常自动处理
 * @author Kevin
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public ResponseEntity<ApiResponse<String>> handleCustomException(CustomException ex) {
        log.error("业务异常: {}", ex.getMessage());
        ApiResponse<String> response = new ApiResponse<>(ex.getCode(), ex.getMessage(), null);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
    }

//    @ExceptionHandler(Exception.class)
//    public ResponseEntity<ApiResponse<String>> handleGlobalException(Exception ex) {
//        log.error("系统异常: {}", ex.getMessage());
//        ApiResponse<String> response = new ApiResponse<>(500, "系统错误: " + ex.getMessage(), null);
//        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
//    }

}
